
<template>
  <div class="table-box">
    <div class="statistics">
      <div class="one">
        <div class="name">当月预估收入</div>
        <div class="num">99999 <span>元</span></div>
      </div>
      <div class="one">
        <div class="name">当月实际收入</div>
        <div class="num">99999 <span>元</span></div>
      </div>
      <div class="one">
        <div class="name">当月预估总成本</div>
        <div class="num">0 <span>元</span></div>
      </div>
      <div class="one">
        <div class="name">当月实际总成本</div>
        <div class="num">0 <span>元</span></div>
      </div>
      <div class="one">
        <div class="name">未结算收入</div>
        <div class="num">0 <span>元</span></div>
      </div>
    </div>
    <!-- 查询表单 -->
    <SearchForm
      v-show="isShowSearch"
      @search="search"
      @reset="reset"
      :search-form-data="searchFormData"
      >
      <template #input>
        <GridItem index="1">
          <el-form-item label="项目名称" prop="project">
            <el-input v-model="searchFormData.project"></el-input>
          </el-form-item>
        </GridItem>
        <GridItem index="2">
          <el-form-item label="站点" prop="site">
            <el-input v-model="searchFormData.site"></el-input>
          </el-form-item>
        </GridItem>
        <GridItem index="3">
          <el-form-item label="时间" prop="date">
            <el-date-picker
              v-model="searchFormData.date"
              type="daterange"
              start-placeholder="开始时间"
              end-placeholder="结束时间"
              @change="pickerChangeFn"
            />
          </el-form-item>
        </GridItem>
        <GridItem index="4">
          <el-form-item label="结算状态" prop="settlement_status">
            <!-- <el-switch v-model="searchFormData.settlement_status" active-value="Y" inactive-value="N" /> -->
          </el-form-item>
        </GridItem>
      </template>
    </SearchForm>
    <!-- 表格主体 -->
    <div class="card table-main">
      <!-- 表格头部 操作按钮 -->
      <div class="table-header">
        <div class="header-button-lf">
          <!-- 新增 -->
          <el-button type="primary" icon="CirclePlus" @click="add">新增</el-button>
        </div>
        <div class="header-button-ri">
          <slot name="toolButton">
            <el-button icon="Search" circle @click="isShowSearch = !isShowSearch"/>
          </slot>
        </div>
      </div>
      <el-table :data="tableData" style="width: 100%" >
        <el-table-column type="index" label="排序" width="100" />
        <el-table-column property="project" label="项目名称" />
        <el-table-column property="site" label="站点" />
        <el-table-column property="count" label="计数" />
        <el-table-column property="revenue_unit_price" label="单价" />
        <el-table-column property="estimated_revenue" label="预估收入" />
        <el-table-column property="actual_revenue" label="实际收入" />
        <el-table-column property="date_range_start" label="开始时间">
          <template #default="scope">
            <span>{{ parseTime(scope.row.date_range_start, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column property="date_range_end" label="结束时间">
          <template #default="scope">
            <span>{{ parseTime(scope.row.date_range_end, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column property="settlement_status" label="结算状态" width="100">
          <template #default="scope">
            <el-switch v-model="scope.row.settlement_status" active-value="Y" inactive-value="N" @click="switchChange(scope.row)" />
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" align="center" width="120">
          <template #default="scope">
            <div class="option-btn">
              <!-- 编辑 -->
              <el-icon class="option-icon" @click="edit(scope.row)"><Edit /></el-icon>
              <!-- 查看 -->
              <el-icon class="option-icon" @click="view(scope.row)"><Document /></el-icon>
              <!-- 删除 -->
              <el-icon class="option-icon" @click="del(scope.row)"><Delete /></el-icon>
            </div>
            
          </template>
        </el-table-column>
      </el-table>
      <!-- <Pagination :total="total" :currentPage="listQuery.page" :pageSize="listQuery.pageSize" @handleSizeChange="handleSizeChange" @handleCurrentChange="handleCurrentChange" /> -->
    </div>
    <AddEdit ref="AddEditRef" @addSave="addSave" @editSave="editSave"></AddEdit>
    <Cost ref="CostRef"></Cost>
  </div>
</template>

<script setup lang="ts" name="strong">
import { ref, reactive, onMounted, watch, onUnmounted, computed } from "vue";
import GridItem from "@/components/Grid/components/GridItem.vue";
// 搜索
import SearchForm from "@/components/SearchForm/index.vue";
import { flowsListApi,flowsSaveApi,flowsEditApi,flowsDelApi} from "@/api/modules/records";
// 分页
// import Pagination from "@/components/Pagination/index.vue";
// 新增、编辑、
import AddEdit from "./components/add_edit.vue";
// 查看
import Cost from "./components/cost.vue";
import { parseTime } from "@/utils/index";
import { ElMessage,ElMessageBox } from "element-plus";

// 搜索数据
const searchFormData = reactive({
  project:'',
  site:'',
  settlement_status: '',
  date:[],
  date_range_start:'',
  date_range_end:'',
});
// 是否显示搜索模块
const isShowSearch = ref(true);
watch(() => searchFormData.date, () => {
  searchFormData.date_range_start = parseTime(searchFormData.date[0],'{y}-{m}-{d}')
  searchFormData.date_range_end = parseTime(searchFormData.date[1],'{y}-{m}-{d}')
})
const pickerChangeFn = (value) => {
  if(value===null){
    searchFormData.date_range_start = ''
    searchFormData.date_range_end = ''
  }
}

const listQuery = reactive({
  page: 1, // 当前页数
  pageSize: 20, // 每页显示条数
  order: 'desc',
  column: 'createdAt'
})
// 总条数
const total = ref(0)
// 每页条数发生改变
const handleSizeChange = (val) => {
  listQuery.pageSize = val
  getTableList()
}
// 页码发生改变
const handleCurrentChange = (val) => {
  listQuery.page = val
  getTableList()
}

onMounted(() => {
  getTableList()
});
let tableData = ref()
const getTableList = () => {
  let { date, ...newObj } = searchFormData;
  flowsListApi({}).then((res:any) => {
    tableData.value = res
    // total.value = res.data.totalCount
  })
};
const search = () => {
  getTableList()
};
const reset = () => {
  searchFormData.date = []
  searchFormData.date_range_start = ''
  searchFormData.date_range_end = ''
  getTableList()
};
// 新增编辑
let AddEditRef = ref()
const add = () => {
  AddEditRef.value.open({title: '新增',disabled: false})
}
// 新增保存
const addSave = (form) => {
  flowsSaveApi(form).then((res:any) => {
    if(res.success) {
      ElMessage.success('新增成功！')
      AddEditRef.value.close()
      getTableList()
    }
  })
}
// 编辑
const edit = (row) => {
  AddEditRef.value.edit({title: '编辑',form: row,disabled: true})
}
// 编辑保存
const editSave = (form) => {
  const data = {
    flow_id: form._id,
    revenue_unit_price: form.revenue_unit_price,
    actual_revenue: form.revenue_unit_price
  }
  flowsEditApi(data).then((res:any) => {
    console.log('res',res);
    
    if(res.success) {
      ElMessage.success('修改成功！')
      AddEditRef.value.close()
      getTableList()
    }
  })
}
const CostRef = ref()
const view = (row) => {
  CostRef.value.open({id:row._id})
}
// 禁用启用
const switchChange = (row) => {
  if(row.activeFlag === 'N') {
    ElMessageBox.confirm('确认切换结算状态吗？', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }).then(() => {
      // consignorDisableApi([row.id]).then((res:any) => {
      //   if(res.success) {
      //     ElMessage.success(i18n.t('M0.M00031'))
      //     getTableList()
      //   }
      // })
    }).catch(() => {
      getTableList()
    })
  } else {
    ElMessageBox.confirm('确认切换结算状态吗？', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }).then(() => {
      // consignorEnableApi([row.id]).then((res:any) => {
      //   if(res.success) {
      //     ElMessage.success(i18n.t('M0.M00033'))
      //     getTableList()
      //   }
      // })
    }).catch(() => {
      getTableList()
    })
  }
}
const del = (row) => {
  ElMessageBox.confirm('确定要删除吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  }).then(() => {
    flowsDelApi(row._id).then((res:any) => {
      if(res.success) {
        ElMessage.success('删除成功！')
        getTableList()
      }
    })
  })
}
</script>
<style lang="scss" scoped>
.statistics {
  width: 100%;
  height: 100px;
  display: flex;
  .one {
    width: 20%;
    height: 80px;
    margin: 10px;
    background-color: #fff;
    border-radius: 10px;
    border: 1px solid #F0F0F0;
    box-shadow: 0rpx 1rpx 4rpx 0rpx rgba(0,0,0,0.05);
    padding: 10px 0 0 20px;
    box-sizing: border-box;
    .name {
      font-size: 18px;
    }
    .num {
      font-size: 24px;
      font-weight: bold;
      span {
        font-size: 18px;
      }
    }
  }
}
.option-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  .option-icon {
    margin: 0 10px;
    cursor: pointer;
  }
}
</style>
